package mdoc.tp1;

import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import domain.Contact;
import domain.ContactGroup;
import domain.Entreprise;
import domain.PhoneNumber;
import domain.DAO.DAOContact;
import domain.DAO.DAOContactGroup;
import domain.DAO.DAOFactory;
import domain.interfaces.IDAOContact;
import domain.interfaces.IDAOContactGroup;
import domain.interfaces.IDAOEntreprise;

/**
 * Servlet implementation class ViewProfile
 */
public class ViewProfile extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ViewProfile() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Long id = Long.valueOf(request.getParameter("userid")).longValue();
		String ctype = request.getParameter("ctype");
		if (id != null) {
			ServletContext ctx = getServletContext();
			if (ctype.equalsIgnoreCase("person")) {
				IDAOContact DAOC = DAOFactory.getDAOContact(ctx);
				Contact c = DAOC.find(id);
				request.setAttribute("Contact", c);
				request.setAttribute("CPhones", retrievePhones(c));

				IDAOContactGroup DAOCG = DAOFactory.getDAOContactGroup(ctx);
				request.setAttribute("ContactGroups",
						retrieveGroups(c.getBooks(), DAOCG.findAll()));

				request.getRequestDispatcher("contactProfile.jsp").forward(
						request, response);
			} else if (ctype.equalsIgnoreCase("company")) {
				IDAOEntreprise DAOE = DAOFactory.getDAOEntreprise(ctx);
				Entreprise e = DAOE.find(id);
				request.setAttribute("Contact", e);
				request.setAttribute("CPhones", retrievePhones(e));

				IDAOContactGroup DAOCG = DAOFactory.getDAOContactGroup(ctx);
				request.setAttribute("ContactGroups",
						retrieveGroups(e.getBooks(), DAOCG.findAll()));

				request.getRequestDispatcher("contactProfile.jsp").forward(
						request, response);
			}
		} else {
			request.getRequestDispatcher("index.jsp")
					.forward(request, response);
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	private Set<PhoneNumber> retrievePhones(Contact c) {
		boolean mobile = false;
		boolean work = false;
		boolean home = false;

		Set<PhoneNumber> setRes = new HashSet<PhoneNumber>(c.getPhones());
		for (PhoneNumber p : setRes) {
			if (p.getPhoneKind().equalsIgnoreCase("Home")) {
				System.out.println("home is true");
				home = true;
			}

			if (p.getPhoneKind().equalsIgnoreCase("Mobile")) {
				System.out.println("mobile is true");
				mobile = true;
			}

			if (p.getPhoneKind().equalsIgnoreCase("Work")) {
				System.out.println("work is true");
				work = true;
			}
		}

		if (!home) {
			System.out.println("adding home");
			PhoneNumber p = new PhoneNumber();
			p.setPhoneKind("Home");
			p.setPhoneNumber("");
			setRes.add(p);
		}

		if (!work) {
			System.out.println("adding work");
			PhoneNumber p = new PhoneNumber();
			p.setPhoneKind("Work");
			p.setPhoneNumber("");
			setRes.add(p);
		}

		if (!mobile) {
			System.out.println("adding mobile");
			PhoneNumber p = new PhoneNumber();
			p.setPhoneKind("Mobile");
			p.setPhoneNumber("");
			setRes.add(p);
		}

		return setRes;
	}

	private Map<ContactGroup, Boolean> retrieveGroups(Set<ContactGroup> cList,
			Set<ContactGroup> fullList) {
		Map<ContactGroup, Boolean> setRes = new Hashtable<ContactGroup, Boolean>();

		for (ContactGroup c : fullList) {
			boolean contains = false;
			for (ContactGroup cl : cList) {
				if (cl.getGroupName().equalsIgnoreCase(c.getGroupName())) {
					contains = true;
				}
			}
			setRes.put(c, contains);
		}
		return setRes;
	}

}
